<?php
	session_start();
	require_once dirname(__FILE__).DIRECTORY_SEPARATOR.'../model/Service/UserService.php';
	require_once dirname(__FILE__).DIRECTORY_SEPARATOR.'../model/Service/ArticleService.php';
    require_once dirname(__FILE__).DIRECTORY_SEPARATOR.'../model/Context/BaseContext.php';
    require_once dirname(__FILE__).DIRECTORY_SEPARATOR.'../model/ServiceResult/ServiceResult.php';
    $userService = new UserService(); 
    $articleService = new ArticleService();
    $context = new BaseContext();
	$result = new ServiceResult();
	if(isset($_GET['type']))
	{
		$type = $_GET['type'];
		if($type == "email")
		{
			if(isset($_POST['email']))
			{
				$email = $_POST['email'];
				$rs = $userService->checkEmailExist($context, $result, $email);
				if($rs == true)
					echo "<span style='color: red;'>( Not Available )</span>";
				else
					echo "<span style='color: green;'>( Available )</span>";
			}
		}
		if($type == "uname")
		{
			if(isset($_POST['uname']))
			{
				$uname = $_POST['uname'];
				$rs = $userService->checkUnameExist($context, $result, $uname);
				if($rs == true)
					echo "<span style='color: red;'>( Not Available )</span>";
				else
					echo "<span style='color: green;'>( Available )</span>";
			}
		}
		
		if($type == "articleCode")
		{
			if(isset($_POST['code']))
			{	
				$code = $_POST['code'];
				$rs = $articleService->checkCodeExist($context, $result, $code);
				if($rs == true)
					echo "<span style='color: red;'>( Code is exist, please input another code!!!! )</span>";
				else
					echo "<span style='color: green;'>( Available )</span>";
			}
		}

		
		if($type == "oldPass")
		{	
			if(isset($_POST['oldPass']))
			{	
				$pass = Tool::md5_base64($_POST['oldPass']);
				if(isset($_SESSION['user']))
				{
				   $id = $_SESSION['user']['ID'];
				}
				$rs = $userService->checkPassExists($context, $result, $pass, $id);
				if($rs == true)
					echo "<span style='color: green;'>( Right ! )</span>";
				else
					echo "<span style='color: red;'>( Wrong)</span>";
			}
		}
	}
?>